﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ImobitechModel.com.Controller;
using ImobitechModel.com.Constantes;
using System.Web.UI.HtmlControls;

namespace Imobitech
{
    public partial class Default : System.Web.UI.Page
    {

        private int RowCount
        {
            get
            {
                return (int)ViewState["RowCount"];
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }

        private int PageCurrent
        {
            get
            {
                return (int)ViewState["Page"];
            }
            set
            {
                ViewState["Page"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FetchData(6, 0);
                CarregaBairro();
                carregarddlTipo();
                carregarddlTipoImovel();
           } 
            else
            {
                //plcPaging.Controls.Clear();
                CreatePagingControl();
            }
        }

        private void CarregaBairro()
        {
            ListItem lst;
            string[] bairros = BairroCuritiba.bairros.Split(',');
            for (int i = 0; i < bairros.Count(); i++)
            {
                string bairro = bairros[i];
                lst = new ListItem(bairro, i.ToString());
                chkList.Items.Add(lst);
            }
        }

        private void FetchData(int take, int pageSize)
        {
            ControladorAnuncio cg = new ControladorAnuncio();
            List<Anunciar> listAnunciar = (List<Anunciar>)cg.retornaImoveis(true, 1);
            PagedDataSource page = new PagedDataSource();
            page.AllowCustomPaging = true;
            page.AllowPaging = true;
            page.DataSource = listAnunciar.Take(take).Skip(pageSize);
            page.PageSize = 6;
            rptAnunciosVenda.DataSource = page;
            rptAnunciosVenda.DataBind();

            if (!IsPostBack)
            {
                RowCount = listAnunciar.Count;
                PageCurrent = 1;
                CreatePagingControl();
            }
        }

        private void CreatePagingControl()
        {

            if (PageCurrent > 1)
            {
                //<li class="prev disabled"><a href="#">&larr; Previous</a></li>
                HtmlGenericControl liLinha = new HtmlGenericControl("li");

                liLinha.Attributes.Add("class", "prev");

                LinkButton lnk = new LinkButton();
                //lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPagePrev";
                lnk.Text = "&larr; Anterior";

                liLinha.Controls.Add(lnk);

                ulpagination.Controls.Add(liLinha);
            }
            else
            {
                HtmlGenericControl liLinha = new HtmlGenericControl("li");

                liLinha.Attributes.Add("class", "prev disabled");

                LinkButton lnk = new LinkButton();
                //lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPagePrev";
                lnk.Text = "&larr; Anterior";

                liLinha.Controls.Add(lnk);

                ulpagination.Controls.Add(liLinha);
            }

            for (int i = 0; i < (RowCount / 6) + 1; i++)
            {
                HtmlGenericControl liLinha = new HtmlGenericControl("li");

                LinkButton lnk = new LinkButton();
                lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();

                liLinha.Controls.Add(lnk);

                ulpagination.Controls.Add(liLinha);
            }

            // && 
            if (((RowCount / 6) > 0) && (PageCurrent < (RowCount / 6)+1))
            {
                // <li class="next"><a href="#">Next &rarr;</a></li>
                HtmlGenericControl liNext = new HtmlGenericControl("li");

                liNext.Attributes.Add("class", "next");

                LinkButton lnkNext = new LinkButton();
                lnkNext.Click += new EventHandler(lbl_Click);
                lnkNext.ID = "lnkPageNext" + (PageCurrent + 1).ToString();
                lnkNext.Text = "Próximo &rarr;";

                liNext.Controls.Add(lnkNext);

                ulpagination.Controls.Add(liNext);
            }
            else
            {
                // <li class="next"><a href="#">Next &rarr;</a></li>
                HtmlGenericControl liNext = new HtmlGenericControl("li");

                liNext.Attributes.Add("class", "next disabled");

                LinkButton lnkNext = new LinkButton();
                //lnk.Click += new EventHandler(lbl_Click);
                lnkNext.ID = "lnkPageNext";
                lnkNext.Text = "Próximo &rarr;";

                liNext.Controls.Add(lnkNext);

                ulpagination.Controls.Add(liNext);
            }

            //if 
            //{
            //    // <li class="next"><a href="#">Next &rarr;</a></li>
            //    HtmlGenericControl liNext = new HtmlGenericControl("li");

            //    liNext.Attributes.Add("class", "next");

            //    LinkButton lnkNext = new LinkButton();
            //    //lnk.Click += new EventHandler(lbl_Click);
            //    lnkNext.ID = "lnkPageNext";
            //    lnkNext.Text = "Próximo &rarr;";

            //    liNext.Controls.Add(lnkNext);

            //    ulpagination.Controls.Add(liNext);
            //}

        }

        void lbl_Click(object sender, EventArgs e)
        {
            LinkButton lnk = sender as LinkButton;
            int currentPage = 1;
            if (lnk.Text == "Próximo &rarr;")
            {
                string textId = lnk.ID;
                currentPage = int.Parse(textId.Replace("lnkPageNext", ""));
            }
            else
            {
                currentPage = int.Parse(lnk.Text);
            }
            PageCurrent = currentPage;
            int take = currentPage * 6;
            int skip = currentPage == 1 ? 0 : take - 6;
            FetchData(take, skip);
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            IDictionary<string, string> anuncio = new Dictionary<string, string>();

            anuncio.Add(NomeCampoConstantes.campo_ImovelCidade, txtCidade.Text);
            anuncio.Add(NomeCampoConstantes.campo_ImovelBairro, txtBairro.Text);
            anuncio.Add(NomeCampoConstantes.campo_ImovelEstado, txtEstado.Text);
            anuncio.Add(NomeCampoConstantes.campo_ImovelTipoAnuncio, ddlTipo.SelectedValue);
            anuncio.Add(NomeCampoConstantes.campo_AnuncioTipoImovel, ddlTipoImovel.SelectedValue);
            Session.Add("Anuncio", anuncio);
            Response.Redirect("/Anuncio/ListaAnuncio.aspx");
            //ControladorAnuncio cg = new ControladorAnuncio();
            //int idImovel = (int)cg.buscaAnuncio(anuncio);
        }

        private void carregarddlTipo()
        {
            ddlTipo.Items.Add(new ListItem("Aluguel", "2"));
            ddlTipo.Items.Add(new ListItem("Venda", "1"));
        }

        private void carregarddlTipoImovel()
        {
            ControladorImovel cg = new ControladorImovel();
            ddlTipoImovel.DataSource = (Dictionary<int, string>)cg.retornaTipoImovel();
            ddlTipoImovel.DataTextField = "Value";
            ddlTipoImovel.DataValueField = "key";
            ddlTipoImovel.DataBind();
        }
    }
}